package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberLease1Binding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberLeaseBean;
import com.sgb.member.ui.activity.MemberBusinessActivity;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.view_model.BusinessLeaseModel1;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.CopyUtils;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.view.selection.XChooseDialog;
import com.swgk.core.view.selection.XChooseEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁经营 供方出租
 */
public class BusinessLeaseFragment1 extends AppLazyFragment implements MemberStatusView.OnChooseListener, XChooseDialog.DialogCallBackImpl {

    @Inject
    public BusinessLeaseModel1 sModel;
    private FragmentMemberLease1Binding mBinding;
    private HomeOtherMemberAdapter<MemberLeaseBean> mAdapter;
    private int mCurPos; // 主页面TAB下标
    private CommonPopEntity mCurEntity; // 主页面TAB下标
    // 经营地址Popup
    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;
    private boolean noMoreData;
    private String flag = "1";//1 ,供方出租 2，公司出租，个人出租

    private String mNo1 = ""; // "类目");
    private String mNo2 = ""; // "分类");
    private String mNo6 = ""; // "地区");
    private String mNo7 = ""; // "市区");
    private String mNo8 = "1"; // "排序");
    private String mNo9 = "";//会员级别

    public static BusinessLeaseFragment1 newInstance() {
        BusinessLeaseFragment1 fragment = new BusinessLeaseFragment1();
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_lease_1, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.chooseLayout.setOnClickListener(v -> {
            for (int i = 0; i < mNameArray.size(); i++) {
                XChooseEntity chooseEntity = mData.get(mData.keyAt(i));
                if (chooseEntity != null) {
                    if (chooseEntity.getList() == null || chooseEntity.getList().size() == 0) {
                        if (chooseEntity.getSort() == PickCateType.TYPE_2 ||
                                chooseEntity.getSort() == PickCateType.TYPE_8 ||
                                chooseEntity.getSort() == PickCateType.TYPE_6) {
                            loadOtherData();
                            return;
                        }
                    }
                }
            }
            mChooseDialog.initData(mData, mFilters);
            mChooseDialog.showDialog();
        });
        initChooseDialog();
        initPopup();
        initRecycler();
        sModel.getLeaseType();
        loadData(false);
        loadOtherData();
    }

    @Override
    protected void onLazyLoad() {

    }



    // 更新顶部Tab数据
    List<CommonPopEntity> tab_data = new ArrayList<>();

    public void setTabLayoutData(List<CommonPopEntity> data) {
        mBinding.tab.setVisibility(View.VISIBLE);
        mBinding.tab.removeAllTabs();
        mBinding.tab.setOnTabSelectedListener(null);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tab.addTab(mBinding.tab.newTab().setText(data.get(i).getTypeName()));
        }
        tab_data.clear();
        tab_data.addAll(data);
        tabSelect(0);
        mBinding.tab.setNormalColor(R.color.color_title_small);
        mBinding.tab.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                mCurPos = pos;
                mCurEntity = tab_data.get(pos);
                mNo1 = mCurEntity.getNo();
                mNo2 = "";
                if (pos == 0) {
                    mBinding.childTab.setVisibility(View.GONE);
                } else {
                    setChildTab();
                }
                loadData(false);
            }
        });
    }

    public void setChildTab() {
        List<CommonPopEntity> children = new ArrayList<>();
        if (mCurEntity.getNo().equals("3")) {
            // 车辆租赁
            if (mCurEntity.getChildren() != null && mCurEntity.getChildren().size() > 0) {
                children = mCurEntity.getChildren().get(0).getChildren();
            }
        } else {
            children = mCurEntity.getChildren();
        }
        setChooseItemData(PickCateType.TYPE_2, children);
    }

    public void loadData(boolean isLoadMore) {
        String text = "";
        try {
            text = ((MemberBusinessActivity) getActivity()).getSearchText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        sModel.rentList(isLoadMore, text, mNo1, mNo2, mNo8, mNo6, mNo7, mNo9);
    }

    private void loadOtherData() {
        if (isListEmpty(PickCateType.TYPE_8)) {
            sModel.getLeaseTimeSort(PickCateType.TYPE_8, false); // 发布时间排序
        }
        if (scrData == null || scrData.size() == 0) {
            sModel.getAddressModel(PickCateType.TYPE_6);  // 经营地址
        }
        if (isListEmpty(PickCateType.TYPE_9)) {
            sModel.getMemberLevelData(PickCateType.TYPE_9, false); // 会员级别
        }
    }

    public void setAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            if (TextUtils.isEmpty(mNo6)) {
                mAddressPop.setData(data, true);
            } else {
                String mName5 = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_NAME, "");
                mAddressPop.setDataAndPosition(data, mName5, "", "", true, true);
                mAddressPop.setDefaultByAreaCode(mAddressPop.getProvincePosition());
                mBinding.popup3.updateChooseTextStatus(1, mName5);
            }
        } else {
            sModel.getAddressModel(PickCateType.TYPE_6);
        }
    }

    private void showChildTab(List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) {
            mBinding.childTab.setVisibility(View.GONE);
            return;
        }
        data.add(0, new CommonPopEntity("", "全部"));
        mBinding.childTab.setVisibility(View.VISIBLE);
        mBinding.childTab.removeAllTabs();
        mBinding.childTab.setOnTabSelectedListener(null);
        for (int i = 0; i < data.size(); i++) {
            mBinding.childTab.addTab(mBinding.childTab.newTab().setText(data.get(i).getTypeName()));
        }
        tabSelect(0);

        mBinding.childTab.setNormalColor(R.color.color_title_small);
        mBinding.childTab.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                XChooseEntity chooseEntity = mData.get(PickCateType.TYPE_2);
                mNo2 = chooseEntity.getItemNo(pos);
                mFilters.put(PickCateType.TYPE_2, pos);
                loadLeaseOtherType(chooseEntity.getSort(), chooseEntity.getList().get(pos), pos, false);
                loadData(false);
            }
        });
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        TextView textView = mBinding.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mBinding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        initRecyclerAdapter();
    }

    private void initRecyclerAdapter() {
        mAdapter = new HomeOtherMemberAdapter<>(getActivity(), R.layout.item_other_lease, null);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberLeaseBean>() {
            @Override
            public void onItemClick(MemberLeaseBean entity, int position) {
                //sModel.getH5Url(entity.getType(), entity.getNo());
                sModel.getH5Url(entity.getType(), entity.getDataId());
            }
        });
        mAdapter.setType(1);
        if (mAdapter != null) {
            mBinding.recyclerView.setAdapter(mAdapter);
        }
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.GONE);
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<MemberLeaseBean> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setNoMore(noMoreData);
        mAdapter.setType(Integer.parseInt(flag));
        closeRecyclerRefreshing();
        this.noMoreData = noMoreData;
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * ------------------------------- Popup ---------------------------------------------
     */
    private void initPopup() {
        mBinding.chooseLayout.setVisibility(View.GONE);
        mBinding.popup1.setVisibility(View.GONE);
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup2.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup3.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupMemberLever.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        initAddressPopup();
    }

//    private boolean isChangeAddress;

    private void initAddressPopup() {
//        mNo6 = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        mBinding.popup3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null) {
                    sModel.getAddressModel(PickCateType.TYPE_6);
                    return;
                }
                mBinding.popLine.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mNo6) || !TextUtils.isEmpty(mNo7)) {
                    mBinding.popup3.updateChooseTextStatus(-2, null);
                } else {
                    mBinding.popup3.updateChooseTextStatus(-1, mBinding.popup3.getKeyName());
                }
                mAddressPop.showPopwindow0(mBinding.popLine);
            }
        });
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                mNo6 = "";
                mNo7 = "";
                mFilters.put(PickCateType.TYPE_6, mAddressPop.getProvincePosition());
                addMainEntity(PickCateType.TYPE_7, new ArrayList<>());
                mBinding.popup3.updateChooseTextStatus(0, mBinding.popup3.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    mNo6 = entity.getProvinceNo();
                    mBinding.popup3.updateChooseTextStatus(1, entity.getProvinceName());
                    mFilters.put(PickCateType.TYPE_6, mAddressPop.getProvincePosition());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        mNo7 = entity.getCityNo();
                        mBinding.popup3.updateChooseTextStatus(1, entity.getCityName());
                        sModel.getAddressCityData(scrData, mNo6, PickCateType.TYPE_7, false);
                        mFilters.put(PickCateType.TYPE_7, mAddressPop.getmCityPosition());
                    }
                }
//                isChangeAddress = true;
                //刷新数据
                loadData(false);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            mBinding.popLine.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mNo6) || !TextUtils.isEmpty(mNo7)) {
                mBinding.popup3.updateChooseTextStatus(1, null);
            } else {
                mBinding.popup3.updateChooseTextStatus(0, mBinding.popup3.getKeyName());
            }
        });
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        if ("排序".equals(mKeyName)) {
            sModel.getLeaseTimeSort(PickCateType.TYPE_8, false);
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        if ("排序".equals(mKeyName)) {
            if (!TextUtils.equals(no, mNo8)) {
                mNo8 = no;
                mFilters.put(PickCateType.TYPE_8, position);
                loadData(false);
            }
        } else if ("会员级别".equals(mKeyName)) {
            mNo9 = entity.getId();
            mFilters.put(PickCateType.TYPE_9, position);
            loadData(false);
        }
    }

    /**
     * ------------------------------------ 侧滑刷选 ----------------------------------------
     */

    private XChooseDialog mChooseDialog;
    // 数据集
    private SparseArray<XChooseEntity> mData = new SparseArray<>();
    // 本地缓存筛选结果
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> mNameArray;
    // 二级分类关联数据
    private List<Integer> mCateOther;

    private void initChooseDialog() {
        mCateOther = new ArrayList<>();
        mCateOther.add(PickCateType.TYPE_2);
        mCateOther.add(PickCateType.TYPE_21);
        mCateOther.add(PickCateType.TYPE_22);
        mCateOther.add(PickCateType.TYPE_23);
        mCateOther.add(PickCateType.TYPE_24);
        mCateOther.add(PickCateType.TYPE_25);
        mNameArray = new SparseArray<>();
        mNameArray.put(PickCateType.TYPE_1, "类目");
        for (Integer integer : mCateOther) {
            mNameArray.put(integer, "分类");
        }
        mNameArray.put(PickCateType.TYPE_6, "地区");
        mNameArray.put(PickCateType.TYPE_7, "市区");
        mNameArray.put(PickCateType.TYPE_8, "发布时间排序");
        mNameArray.put(PickCateType.TYPE_9, "会员级别");
        mChooseDialog = new XChooseDialog(getContext(), mNameArray);
        mChooseDialog.setCallBack(this);
        for (int i = 0; i < mNameArray.size(); i++) {
            int key = mNameArray.keyAt(i);
            addMainEntity(key, new ArrayList<>());
        }
    }

    public void setChooseItemData(int type, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        addMainEntity(type, data);
        switch (type) {
            case PickCateType.TYPE_2:
                showChildTab(data);
                break;
            case PickCateType.TYPE_8:
                mBinding.popup2.setData(data, mNo8);
                break;
            case PickCateType.TYPE_9:
                mBinding.popupMemberLever.setData(data, mNo9);
                break;
        }
    }

    public void showLeftDialog(int type, List<CommonPopEntity> data) {
        mChooseDialog.addEntity(type, data);
        mChooseDialog.showDialog();
    }

    public void addMainEntity(int index, List<CommonPopEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setList(new ArrayList<>());
            } else {
                mData.get(index).setList(list);
            }
        } else {
            mData.put(index, new XChooseEntity(index, mNameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    public void addMainEntity(int index, String name, List<CommonPopEntity> list) {
        if (!TextUtils.isEmpty(name)) {
            mNameArray.put(index, name);
        }
        if (mData.get(index) != null) {
            mData.get(index).setTypeName(mNameArray.get(index));
            if (list == null || list.size() == 0) {
                mData.get(index).setList(new ArrayList<>());
            } else {
                mData.get(index).setList(list);
            }
        } else {
            mData.put(index, new XChooseEntity(index, mNameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    private boolean isListEmpty(int catType) {
        return mData.get(catType) != null &&
                (mData.get(catType).getList() == null || mData.get(catType).getList().size() == 0);
    }

    @Override
    public void loadChildData(XChooseEntity entity, int pos) {
        // 资质类型当前不需要联动
        switch (entity.getSort()) {
            case PickCateType.TYPE_1:
                loadLeaseType(entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            case PickCateType.TYPE_2:
            case PickCateType.TYPE_21:
            case PickCateType.TYPE_22:
            case PickCateType.TYPE_23:
            case PickCateType.TYPE_24:
            case PickCateType.TYPE_25:
                loadLeaseOtherType(entity.getSort(), entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            case PickCateType.TYPE_6:
                updateCityData(entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            default:
                break;
        }
    }

    private void loadLeaseType(CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        String name1 = "类别";
        String name2 = "类型";
        String name3 = "属性分类";
        if (isShowLeftDialog) {
            mChooseDialog.addEntity(PickCateType.TYPE_2, name1, new ArrayList<>());
            mChooseDialog.addEntity(PickCateType.TYPE_21, name2, new ArrayList<>());
            mChooseDialog.addEntity(PickCateType.TYPE_22, name3, new ArrayList<>());
            for (Integer integer : mCateOther) {
                mChooseDialog.addEntity(integer, new ArrayList<>());
            }
        } else {
            addMainEntity(PickCateType.TYPE_2, name1, new ArrayList<>());
            addMainEntity(PickCateType.TYPE_21, name2, new ArrayList<>());
            addMainEntity(PickCateType.TYPE_22, name3, new ArrayList<>());
            for (Integer integer : mCateOther) {
                addMainEntity(integer, new ArrayList<>());
            }
        }
        if (pos > 0) {
            if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                    entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                }
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(PickCateType.TYPE_2, entity.getChildren());
                } else {
                    addMainEntity(PickCateType.TYPE_2, entity.getChildren());
                }
            } else {
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(PickCateType.TYPE_2, new ArrayList<>());
                } else {
                    addMainEntity(PickCateType.TYPE_2, new ArrayList<>());
                }
            }
        }
    }

    private void loadLeaseOtherType(int sort, CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        ArrayList<CommonPopEntity> objects = new ArrayList<>();
        for (int i = 0; i < mCateOther.size(); i++) {
            Integer integer = mCateOther.get(i);
            if (integer > sort) {
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(integer, objects);
                } else {
                    addMainEntity(integer, objects);
                }
            }
        }
        if (pos > 0 && mCateOther.contains(sort + 1)) {
            int index = mCateOther.indexOf(sort) + 1;
            if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                    entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                }
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(mCateOther.get(index), entity.getChildren());
                } else {
                    addMainEntity(mCateOther.get(index), entity.getChildren());
                }
            } else {
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(mCateOther.get(index), objects);
                } else {
                    addMainEntity(mCateOther.get(index), objects);
                }
            }
        }
    }

    private void updateMultiCategoryData(int sort, CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        ArrayList<CommonPopEntity> objects = new ArrayList<>();
        if (isShowLeftDialog) {
            switch (sort) {
                case PickCateType.TYPE_2:
                    mChooseDialog.addEntity(PickCateType.TYPE_21, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_22, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_23, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_24, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_21:
                    mChooseDialog.addEntity(PickCateType.TYPE_22, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_23, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_24, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_22:
                    mChooseDialog.addEntity(PickCateType.TYPE_23, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_24, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_23:
                    mChooseDialog.addEntity(PickCateType.TYPE_24, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_24:
                    mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_25:
                    break;
            }
            if (pos > 0) {
                switch (sort) {
                    case PickCateType.TYPE_2:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            mChooseDialog.addEntity(PickCateType.TYPE_21, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_21, objects);
                        }
                        break;
                    case PickCateType.TYPE_21:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            mChooseDialog.addEntity(PickCateType.TYPE_22, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_22, objects);
                        }
                        break;
                    case PickCateType.TYPE_22:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            mChooseDialog.addEntity(PickCateType.TYPE_23, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_23, objects);
                        }
                        break;
                    case PickCateType.TYPE_23:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            mChooseDialog.addEntity(PickCateType.TYPE_24, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_24, objects);
                        }
                        break;
                    case PickCateType.TYPE_24:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            mChooseDialog.addEntity(PickCateType.TYPE_25, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_25, objects);
                        }
                        break;
                    case PickCateType.TYPE_25:
                        break;
                }
            }
        } else {
            switch (sort) {
                case PickCateType.TYPE_2:
                    addMainEntity(PickCateType.TYPE_21, objects);
                    addMainEntity(PickCateType.TYPE_22, objects);
                    addMainEntity(PickCateType.TYPE_23, objects);
                    addMainEntity(PickCateType.TYPE_24, objects);
                    addMainEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_21:
                    addMainEntity(PickCateType.TYPE_22, objects);
                    addMainEntity(PickCateType.TYPE_23, objects);
                    addMainEntity(PickCateType.TYPE_24, objects);
                    addMainEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_22:
                    addMainEntity(PickCateType.TYPE_23, objects);
                    addMainEntity(PickCateType.TYPE_24, objects);
                    addMainEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_23:
                    addMainEntity(PickCateType.TYPE_24, objects);
                    addMainEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_24:
                    addMainEntity(PickCateType.TYPE_25, objects);
                    break;
                case PickCateType.TYPE_25:
                    break;
            }
            if (pos > 0) {
                switch (sort) {
                    case PickCateType.TYPE_2:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            addMainEntity(PickCateType.TYPE_21, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_21, objects);
                        }
                        break;
                    case PickCateType.TYPE_21:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            addMainEntity(PickCateType.TYPE_22, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_22, objects);
                        }
                        break;
                    case PickCateType.TYPE_22:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            addMainEntity(PickCateType.TYPE_23, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_23, objects);
                        }
                        break;
                    case PickCateType.TYPE_23:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            addMainEntity(PickCateType.TYPE_24, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_24, objects);
                        }
                        break;
                    case PickCateType.TYPE_24:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            addMainEntity(PickCateType.TYPE_25, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_25, objects);
                        }
                        break;
                    case PickCateType.TYPE_25:
                        break;
                }
            }
        }

    }

    private void updateCityData(CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        mChooseDialog.addEntity(PickCateType.TYPE_7, new ArrayList<>());
        if (pos > 0) {
            sModel.getAddressCityData(scrData, entity.getNo(), PickCateType.TYPE_7, isShowLeftDialog);
        }
    }

    @Override
    public void result(SparseArray<XChooseEntity> data, SparseIntArray map) {
        mData = CopyUtils.deepPopEntityCopy(data);
        mFilters = map.clone();
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            refreshItemValue(key, mFilters.get(key), true);
        }
        loadData(false);
    }

    private void refreshItemValue(int key, int value, boolean isDialog) {
        XChooseEntity entity = mData.get(key);
        mFilters.put(key, value);
        String itemNo = entity.getItemNo(value);
        switch (key) {
            case PickCateType.TYPE_1:
                if (mCurPos > 0) {
                    mNo1 = itemNo;
                }
                break;
            case PickCateType.TYPE_2:
                mNo2 = itemNo;
                mBinding.popup1.refreshChoose(mNo2, entity.getItemName(value), value);
                tabSelect(value);
                break;
            case PickCateType.TYPE_21:
            case PickCateType.TYPE_22:
            case PickCateType.TYPE_23:
            case PickCateType.TYPE_24:
            case PickCateType.TYPE_25:
                if (value > 0) {
                    mNo2 = itemNo;
                }
                break;
            case PickCateType.TYPE_6:
                mNo6 = itemNo;
                mBinding.popup3.refreshChoose(mNo6, entity.getItemName(value), value);
                mAddressPop.setDefPosition(value, 0);
                break;
            case PickCateType.TYPE_7:
                mNo7 = itemNo;
                mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_6), value);
                if (value > 0) {
                    mBinding.popup3.refreshChoose(mNo7, entity.getItemName(value), value);
                }
                break;
            case PickCateType.TYPE_8:
                mNo8 = itemNo;
                mBinding.popup2.refreshChoose(mNo8, entity.getItemName(value), value);
                break;
            case PickCateType.TYPE_9:
                mNo9 = entity.getItemNo(value);
                mBinding.popupMemberLever.refreshChoose(mNo9, entity.getItemName(value), value);
                break;
            default:
                break;
        }
    }

    @Override
    public void reset() {
        mFilters.clear();
        resetMainData();
        result(mData, mFilters);
        mChooseDialog.initData(mData, mFilters);
        mChooseDialog.showDialog();
    }

    private void resetMainData() {
        if (mCurPos == 0) {
            addMainEntity(PickCateType.TYPE_2, new ArrayList<>());
        }
        addMainEntity(PickCateType.TYPE_21, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_22, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_23, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_24, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_25, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_7, new ArrayList<>());
        tabSelect(0);
    }


    private void tabSelect(int pos) {
        if (mBinding.childTab.getTabCount() > 0) {
            mBinding.childTab.getTabAt(pos).select();
        }
    }

    @Override
    public void onCancel() {
        mChooseDialog.initData(mData, mFilters);
    }
}
